public class LeetCode {
    public ListNode removeElements(ListNode head, int val){
        while (head != null && head.val == val){
            ListNode delNode = head;
            head = head.next;
            delNode.next = null;
        }
        if (head == null){
            return null;
        }
        ListNode  preNode = head;
        while(preNode.next != null){
            if (preNode.next.val == val){
                ListNode delNode = preNode.next;
                preNode.next = delNode.next;
                delNode.next = null;
            }else{
                preNode = preNode.next;
            }
        }
        return head;
    }



    public ListNode removeElement2(ListNode head, int val){
        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode prev = dummyHead;
        while(prev.next != null){
            if (prev.next.val == val){
                prev.next = prev.next.next;
            }else{
                prev = prev.next;
            }
        }
        return dummyHead.next;
    }
}
